package com.zl.tby.vo;

import com.zl.tby.core.base.bean.OptionType;
import com.zl.tby.core.base.util.ToolsUtil;
import com.zl.tby.model.TbyVillageInfo;
import lombok.Data;
import org.noear.solon.validation.annotation.Length;
import org.noear.solon.validation.annotation.NotBlank;
import org.noear.solon.validation.annotation.NotEmpty;
import org.noear.solon.validation.annotation.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * 社区共管表单bean
 * @author sunjiaxing
 * @version 1.0
 * @since 2024/8/22 00:28
 */
@Data
public class TbyVillageInfoForm {

    /**主键id*/
    private Long id;
    /**村庄名称*/
    @NotBlank(message = "村庄名称不能为空")
    @Length(max = 20, message = "村庄名称最大长度为20")
    private String villageName;
    /**经度*/
    @NotBlank(message = "经度不能为空")
    @Length(max = 32, message = "经度最大长度为32")
    private String longitude;
    /**纬度*/
    @NotBlank(message = "纬度不能为空")
    @Length(max = 32, message = "纬度最大长度为32")
    private String latitude;
    /**类型*/
    @NotNull(message = "类型不能为空")
    private Integer villageType;
    /**所属行政村*/
    private Long parentId;
    /**户籍人口*/
    @NotNull(message = "户籍人口不能为空")
    private Long registeredPopulation;
    /**常住人口*/
    @NotNull(message = "常住人口不能为空")
    private Long permanentPopulation;
    /**劳动力*/
    @NotNull(message = "劳动力不能为空")
    private Long labourForce;
    /**介绍*/
    private String content;

    public TbyVillageInfo createEntity() {
        TbyVillageInfo entity = new TbyVillageInfo();
        entity.setVillageName(this.getVillageName());
        entity.setVillageType(this.getVillageType());
        entity.setLongitude(this.getLongitude());
        entity.setLatitude(this.getLatitude());
        entity.setParentId(this.getParentId());
        entity.setRegisteredPopulation(this.getRegisteredPopulation());
        entity.setPermanentPopulation(this.getPermanentPopulation());
        entity.setLabourForce(this.getLabourForce());
        entity.setContent(this.getContent());
        return entity;
    }

    public void updateEntity(TbyVillageInfo entity) {
        entity.setVillageName(this.getVillageName());
        entity.setVillageType(this.getVillageType());
        entity.setLongitude(this.getLongitude());
        entity.setLatitude(this.getLatitude());
        entity.setParentId(this.getParentId());
        entity.setRegisteredPopulation(this.getRegisteredPopulation());
        entity.setPermanentPopulation(this.getPermanentPopulation());
        entity.setLabourForce(this.getLabourForce());
        entity.setContent(this.getContent());
    }

    public static List<OptionType> recurRouteOptions(Long parentId, List<TbyVillageInfo> dataList) {
        List<OptionType> resultList = ToolsUtil.emptyIfNull(dataList).stream()
                .filter(bean -> bean.getParentId().equals(parentId))
                .map(bean -> new OptionType(bean.getId(), bean.getVillageName(), recurRouteOptions(bean.getId(), dataList)))
                .collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
        return resultList;
    }
}
